<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/8
 * Time: 15:06
 * 特惠商品案列
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Caseh extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $case_title = input('case_title');
            
            $where['case_type'] = 2;
            if(isset($case_title) && $case_title != ''){
                $where['case_title'] = ['like', '%' . $case_title . '%'];
            }
            // 时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['case_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['case_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['case_time'] = ['<', $end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('case')->where($where)->count();// 获取总条数
            $lists = Db::name('case')
                ->field('case_id, case_thumb, case_title, case_view, case_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('case_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加案例
     * @return mixed|\think\response\Json
     */
    public function caseAdd(){
        if(Request::instance()->isPost()){
            $case_title = trim(input('case_title'));
            $case_thumb = input('case_thumb');
            $case_images = input('case_images');
            $case_content = input('case_content');
            
            $insert = [
                'case_type' => 2,
                'case_thumb' => $case_thumb,
                'case_images' => $case_images,
                'case_title' => $case_title,
                'case_content' => $case_content,
                'case_time' => date('Y-m-d H:i:s'),
                'case_date' => date('Y-m-d')
            ];
            $res = Db::table('up_case')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('caseh/caseadd');
    }
    
    /**
     * 编辑案例
     * @return mixed|\think\response\Json
     */
    public function caseEdit(){
        $case_id = input('case_id');
        if(Request::instance()->isPost()){
            $case_title = trim(input('case_title'));
            $case_thumb = input('case_thumb');
            $case_images = input('case_images');
            $case_content = input('case_content');
    
            $update = [
                'case_thumb' => $case_thumb,
                'case_images' => $case_images,
                'case_title' => $case_title,
                'case_content' => $case_content,
                'case_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_case')->where('case_id', $case_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $case = Db::table('up_case')
            ->where('case_id', $case_id)
            ->field('case_thumb, case_images, case_title, case_content')
            ->find();
        $this->assign([
            'case' => $case,
            'case_id' => $case_id
        ]);
        return $this->fetch('caseh/caseedit');
    }
    
    /**
     * 案例详情
     * @return mixed
     */
    public function caseDetail(){
        $case_id = input('case_id');
        $case = Db::table('up_case')
            ->field('case_title, case_thumb, case_images, case_content, case_view')
            ->where('case_id', $case_id)
            ->find();
        if(!empty($case['case_images'])){
            $case['case_images'] = explode(',', $case['case_images']);
        }else{
            $case['case_images'] = [];
        }
        return $this->fetch('caseh/casedetail', ['case' => $case]);
    }
    
    /**
     * 删除案例
     * @return \think\response\Json
     */
    public function caseDel(){
        $case_id = input('id');
        $res = Db::table('up_case')->delete($case_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}